#include "admin.h"

void print_book(book temp)
{
    cout <<"书名:"<<temp.get_book_name();
    cout <<"\t作者:"<< temp.get_author() ;
    cout <<"\t价格:"<<temp.get_price() ;
    string str1 = "借出";
    string str2 = "在馆";
    cout << temp.get_is_sta() ? str1: str2;

    cout << endl;
    
}

admin::admin()
    :book_v(MAX_BOOK_NUM)
{

}

admin::~admin()
{

}

void admin::add_book(const book &new_book)
{
    book_v.push_back(new_book);
}

void admin::delete_book(const string &book_name)
{
    this->book_name = book_name;
    
    book temp_book;    
    temp_book.set_book_name(book_name);

    auto it = find(this->book_v.begin(),this->book_v.end(),temp_book);
    
    cout << it->get_author()<< endl;

    //删除
    book_v.erase(it);
}

void admin::modify_book(const string &book_name , const book &updated_book)
{
    //查找
    book temp_book;
    temp_book.set_book_name(book_name);
    auto it = find(this->book_v.begin(),this->book_v.end(),temp_book);
    
    it->set_book_name(updated_book.get_book_name());
    it->set_author(updated_book.get_author());
    it->set_price(updated_book.get_price());
    it->set_is_sta(updated_book.get_is_sta());

}

book admin::search_book(const string &book_name)
{
    //查找
    book temp_book;
    temp_book.set_book_name(book_name);

    auto it = find(this->book_v.begin(),this->book_v.end(), temp_book);

    return *it ;
}

void admin::view_all_books() const
{
    for_each(this->book_v.begin(),this->book_v.end(),print_book);   
}

bool admin::find_book_name(book book_temp)
{
    return book_temp.get_book_name() == this->book_name;
}